package ca.jsbr.as2js_lib.display.canvas.view 
{
	import ca.jsbr.as2js_lib.display.canvas.CanvasStage;
	import ca.jsbr.as2js_lib.display.canvas.View;
	import ca.jsbr.as2js_lib.utils.geom.Rect;
	import js.element.canvas.CanvasRenderingContext2D;
	/**
	 * ...
	 * @author jsbr [jsricard@lvlstudio.com]
	 */
	public class TextView extends View
	{
		static public const LEFT:String = "left";
		static public const RIGHT:String = "right";
		static public const CENTER:String = "center";
		
		private var _font:String;
		private var _fontFamily:String;
		private var _bold:Boolean;
		private var _italic:Boolean;
		private var _textColor:String;
		private var _fontSize:Number;
		private var _text:String;
		private var _multiline:Boolean;
		private var _fixWidth:Boolean;
		private var _fixHeight:Boolean;
		private var _lineSpace:Number;
		
		private var _lines:Array;
		private var _splitText:Array;
		private var _len:uint;
		private var _textAlign:String;
		
		public function TextView() 
		{
			_fontFamily = "Sans-serif";
			textColor = "#000000";
			_fontSize = 12;
			_lineSpace = 2;
			_textAlign = LEFT;
			font();
			
		}
		
		
		override public function get width():Number 
		{
			return _width;
		}
		
		override public function set width(value:Number):void 
		{
			_width = value;
			_fixWidth = true;
		}
		
		override public function get height():Number 
		{
			return _height;
		}
		
		override public function set height(value:Number):void 
		{
			_height = value;
			_fixHeight = true;
		}
		
		private function font():void
		{
			_font = "";
			if(_italic)
				_font += "italic ";
			else
				_font += "normal ";
			if(_bold)
				_font += "bold ";
			else
				_font += "normal ";
			_font += _fontSize + "px ";
			_font += _fontFamily + " ";
			recalText();
		}
		

		public function get bold():Boolean 
		{
			return _bold;
		}
		
		public function set bold(value:Boolean):void 
		{
			_bold = value;
			font();
		}
		
		public function get italic():Boolean 
		{
			return _italic;
		}
		
		public function set italic(value:Boolean):void 
		{
			_italic = value;
			font();
		}
		
		public function get textColor():String 
		{
			return _textColor;
		}
		
		public function set textColor(value:String):void 
		{
			_textColor = value;
			font();
		}
		
		public function get fontSize():Number 
		{
			return _fontSize;
		}
		
		public function set fontSize(value:Number):void 
		{
			_fontSize = value;
			font();
		}
		
		public function get fontFamily():String 
		{
			return _fontFamily;
		}
		
		public function set fontFamily(value:String):void 
		{
			_fontFamily = value;
			font();
		}
		
		public function get text():String 
		{
			if(_text)
				return _text;
			else
				return "";
		}
		
		public function set text(value:String):void 
		{
			_text = value;
			
			if (_multiline) {
				_splitText = _text.split(" ");
				_len = _splitText.length;
				
			}
			recalText();
		}
		
		public function get multiline():Boolean 
		{
			return _multiline;
		}
		
		public function set multiline(value:Boolean):void 
		{
			_multiline = value;
			
			if (_text)
			{
				_splitText = _text.split(" ");
				_len = _splitText.length;
			}
			recalText();
		}
		

		public function fixSize(width:Boolean,height:Boolean):void 
		{
			_fixWidth = width;
			_fixHeight = height;
		}
		
		public function get lineSpace():Number 
		{
			return _lineSpace;
		}
		
		public function set lineSpace(value:Number):void 
		{
			_lineSpace = value;
		}
		
		public function get textAlign():String 
		{
			return _textAlign;
		}
		
		public function set textAlign(value:String):void 
		{
			_textAlign = value;
		}
		
		private function recalText():void
		{
			if (!_text)
				return;
			_lines = null;
			var ctx:CanvasRenderingContext2D = CanvasStage.instance.context;
			if (!_fixWidth)
			{
				ctx.fillStyle = "#" + _textColor;
				ctx.font = _font;
				ctx.textBaseline = 'top';
				_width = ctx.measureText(_text).width;
			}
			if (!_fixHeight)
			{
				_height = _fontSize;
				if (!_lineSpace)
					_lineSpace = 2;
				if (multiline)
				{
					createLines();
				}
			}
		}
		
		private function createLines():void
		{
			_lines = [];
			var line:String = "";
			var ctx:CanvasRenderingContext2D = CanvasStage.instance.context;
			for (var i = 0; i < _len; i++)
			{

				var w = ctx.measureText(line +" " + _splitText[i]).width;
				
				if (i)
					line += " ";
				if (_splitText[i].indexOf("\n") != -1)
				{
					
					var breakLine = _splitText[i].split("\n");
					
					for (var i = 0; i < breakLine.length-1; i++)
					{
						line += breakLine[i];
						_lines.push(line);
						line=""
					}
					line += breakLine[i];
					continue;
				}	
				else if (i == _len - 1) {
					line += _splitText[i];
					w += _width;
				}
				
				if (w < _width)
					line += _splitText[i];
				else if(line!="") {
					_lines.push(line);
					line = _splitText[i];
				}
			}
			_len = _lines.length;
			_height=_len * (this.fontSize + _lineSpace)
		}
		
		override public function render(ctx:CanvasRenderingContext2D, view:Rect = null):void 
		{
			
			if (!_text)
				return;
			ctx.fillStyle = _textColor;

			ctx.font = _font;
			ctx.textBaseline = 'top';
			ctx.textAlign = _textAlign;
			if (!multiline)
			{
				ctx.fillText(_text, stageX, stageY);
			}
			else if (_lines)
			{
				for (var i = 0; i < _len; i++)
				{
					ctx.fillText(_lines[i], stageX, stageY+i * (this.fontSize+_lineSpace));
				}
			}
		}
		
		
	}

}